/*
============================================================================
FileName	:	XQChatRenrenClient.cpp
Author		:	liyake
Website		:	http://www.liyake.com
Create Data	:	2012-03-14 11:26
Change Data	:	2012-03-14 11:27
Detail		:	The renren socket client of xqchat application
============================================================================
*/
#include "XQChatRenrenClient.h"
#include "XQChatRenrenRoster.h"
#include "XQChatClientManager.h"
#include <QtGui>

using namespace XQChat;

XQChat::CXQChatRenrenClient::CXQChatRenrenClient( const CXQChatConfiguration& config )
	: CXQChatClient(config)
{

}

XQChat::CXQChatRenrenClient::~CXQChatRenrenClient( void )
{

}

void XQChat::CXQChatRenrenClient::onRosterReceived()
{
	CXQChatRosterManager* pRosterManager = CXQChatClientManager::getQXChatClientManager()->getRosterManager();

	QStringList listJids = m_pRosterManager->getRosterBareJids();
    foreach(const QString& jid, listJids)
	{
		XQChat::CXQChatRenrenRoster* pRoster = new XQChat::CXQChatRenrenRoster(jid,this);
		if(!pRosterManager->addRoster(pRoster))
		{
			delete pRoster;
			continue;
		}
	}

	emit rosterReceived();
}

void XQChat::CXQChatRenrenClient::onPresenceChanged( const QString& bareJid, const QString& resource )
{
	QXmppPresence presence = m_pRosterManager->getPresence(bareJid,resource);
	CXQChatRosterManager* pRosterManager = CXQChatClientManager::getQXChatClientManager()->getRosterManager();
	QList<CXQChatRoster*> listRosters = pRosterManager->getRostersByBareJid(bareJid);
	
	QString qsHeaderIcon,qsNick,qsRichStatus;
	int iVip=-2;
	QXmppElementList listExtensions = presence.extensions();
    foreach(const QXmppElement& ele, listExtensions)
	{
		if(ele.tagName() == "x")
		{
			QXmppElement elePhoto = ele.firstChildElement("PHOTO").firstChildElement("EXTVAL");
			if(!elePhoto.isNull())
			{
				qsHeaderIcon = elePhoto.value();
				presence.setPhotoHash(QByteArray::fromHex(qsHeaderIcon.toAscii()));
				presence.setVCardUpdateType(QXmppPresence::VCardUpdateValidPhoto);
			}
		}
		else if(ele.tagName() == "nick")
		{
			qsNick = ele.value();
		}
		else if(ele.tagName() == "richstatus")
		{
			qsRichStatus = ele.value();
		}
		else if(ele.tagName() == "vip")
		{
			iVip = ele.value().toInt();
		}
		else
		{
			QMessageBox::information(0,ele.tagName(),ele.value());
		}
	}

    foreach(CXQChatRoster* pRoster, listRosters)
	{
		if(pRoster->getXQChatClient()==this)
		{
			CXQChatRenrenRoster* pRenrenRoster = reinterpret_cast<CXQChatRenrenRoster*>(pRoster);
			if(!qsHeaderIcon.isEmpty())
				pRenrenRoster->setHeaderUrl(qsHeaderIcon);
			if(!qsNick.isEmpty())
				pRenrenRoster->setNick(qsNick);
			if(!qsRichStatus.isEmpty())
				pRenrenRoster->setRichStatus(qsRichStatus);
			if(iVip>-2)
				pRenrenRoster->setVIP(iVip);
		}
		pRoster->setPresence(resource,presence);

		emit presenceChanged(pRoster->getFullJid(),resource);
	}
}

void XQChat::CXQChatRenrenClient::onRosterChanged( const QString& bareJid )
{

}

void XQChat::CXQChatRenrenClient::onSubscriptionReceived( const QString& bareJid )
{

}
